﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Data.SqlClient;

public partial class webadmin_pages_VetinhSan_frm : System.Web.UI.Page
{
    public string act = "";
    private Common comm = new Common();
    public string id = "";
    public int iLoaiFileUpload;
    public static string ma_trung_tam = "";
    public string mess = "";
    public string messnotID = "";
    protected void btn_vetinh_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] sqlparam = new SqlParameter[8];
            sqlparam[0] = new SqlParameter("@ten_trung_tam", this.txt_vt_ten.Text);
            sqlparam[1] = new SqlParameter("@dia_chi_chi_tiet", this.txt_vt_diachi.Text);
            sqlparam[2] = new SqlParameter("@dia_chi", "");
            sqlparam[4] = new SqlParameter("@mo_ta", this.txt_san_mota.Text);
            sqlparam[5] = new SqlParameter("@gioi_thieu", this.txt_vt_gioithieu.Text);
            sqlparam[6] = new SqlParameter("@email", this.txt_vt_email.Text);
            sqlparam[7] = new SqlParameter("@dien_thoai", this.txt_vt_dienthoai.Text);
            if (this.act.Trim().Equals("add"))
            {
                sqlparam[3] = new SqlParameter("@ve_tinh", 1);
                if (this.comm.ExeceuteByStoreProcedure(sqlparam, "TRUNGTAMVIECLAM_ADD") > 0)
                {
                    base.Response.Redirect("sanvieclam_list.aspx");
                }
            }
            else
            {
                sqlparam[3] = new SqlParameter("@ma_trung_tam", this.txtMa.Text);
                if (this.comm.ExeceuteByStoreProcedure(sqlparam, "TRUNGTAMVIECLAM_UPDATE") > 0)
                {
                    base.Response.Redirect("sanvieclam_list.aspx");
                }
            }
        }
        catch (Exception exception)
        {
            this.mess = exception.Message;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] sqlparam = new SqlParameter[8];
            
            sqlparam[0] = new SqlParameter("@ten_san", this.txtTieuDe.Text);
            sqlparam[1] = new SqlParameter("@ngay_dien_ra", this.txt_ngaydienra_RadDateInput.SelectedDate.Value);
            sqlparam[2] = new SqlParameter("@kich_hoat", this.chkKichHoat.Checked ? 1 : 0);
            sqlparam[3] = new SqlParameter("@gioi_thieu", this.txt_san_gioithieu.Text);
            sqlparam[4] = new SqlParameter("@mo_ta", this.txt_san_mota.Text);
            sqlparam[5] = new SqlParameter("@nguoi_tao", this.Session["UserID"]);
            if (this.chkKichHoat.Checked)
            {
                if (ma_trung_tam.Trim().Equals(""))
                {
                    this.comm.execNoneQuery("update SanGiaoDich set kich_hoat = 0 where ma_trung_tam = " + this.drop_san_vetinh.SelectedValue);
                }
                else
                {
                    this.comm.execNoneQuery("update SanGiaoDich set kich_hoat = 0 where ma_trung_tam = " + ma_trung_tam);
                }
            }
            if (this.act.Trim().Equals("add"))
            {
                sqlparam[6] = new SqlParameter("@ma_trung_tam", this.drop_san_vetinh.SelectedValue);
                sqlparam[7] = new SqlParameter("@ma_san", comm.GenerateID());
                this.comm.ExeceuteByStoreProcedure(sqlparam, "SANGIAODICH_ADD");
            }
            else
            {
                sqlparam[6] = new SqlParameter("@ma_trung_tam", this.drop_san_vetinh.SelectedValue);
                sqlparam[7] = new SqlParameter("@ma_san", this.txtMa.Text);
                this.comm.ExeceuteByStoreProcedure(sqlparam, "SANGIAODICH_UPDATE");
            }
            base.Response.Redirect("sanvieclam_list.aspx");
        }
        catch (Exception exception)
        {
            this.mess = exception.Message;
        }
    }

    private void LoadInfo(string id)
    {
        DataSet set = this.comm.ExeceuteDataset("select * from SanGiaoDich where ma_san = '" + id + "'");
        if (set.Tables[0].Rows.Count > 0)
        {
            this.txt_ngaydienra_RadDateInput.SelectedDate = new DateTime?((DateTime)set.Tables[0].Rows[0]["ngay_dien_ra"]);
            this.txt_san_gioithieu.Text = set.Tables[0].Rows[0]["gioi_thieu"].ToString();
            this.txt_san_mota.Text = set.Tables[0].Rows[0]["mo_ta"].ToString();
            this.txtMa.Text = set.Tables[0].Rows[0]["ma_san"].ToString();
            this.txtTieuDe.Text = set.Tables[0].Rows[0]["ten_san"].ToString();
            if ((bool)set.Tables[0].Rows[0]["kich_hoat"])
            {
                this.chkKichHoat.Checked = true;
            }
            else
            {
                this.chkKichHoat.Checked = false;
            }
            ma_trung_tam = set.Tables[0].Rows[0]["ma_trung_tam"].ToString();
            this.drop_san_vetinh.Items.FindByValue(ma_trung_tam).Selected = true;
            this.drop_san_vetinh.Enabled = false;
        }
    }

    private void LoadInfoVeinh(string id)
    {
        DataSet set = this.comm.ExeceuteDataset("select * from TrungTamViecLam where ma_trung_tam = " + id + "");
        if (set.Tables[0].Rows.Count > 0)
        {
            this.txt_vt_diachi.Text = set.Tables[0].Rows[0]["dia_chi_chi_tiet"].ToString();
            this.txt_vt_dienthoai.Text = set.Tables[0].Rows[0]["dien_thoai"].ToString();
            this.txt_vt_email.Text = set.Tables[0].Rows[0]["email"].ToString();
            this.txt_vt_gioithieu.Text = set.Tables[0].Rows[0]["gioi_thieu"].ToString();
            this.txt_vt_mota.Text = set.Tables[0].Rows[0]["mo_ta"].ToString();
            this.txt_vt_ten.Text = set.Tables[0].Rows[0]["ten_trung_tam"].ToString();
        }
    }

    private void LoadVetinh()
    {
        this.drop_san_vetinh.DataSource = this.comm.ExeceuteDataset("select * from TrungTamViecLam where ve_tinh = '1'");
        this.drop_san_vetinh.DataTextField = "ten_trung_tam";
        this.drop_san_vetinh.DataValueField = "ma_trung_tam";
        this.drop_san_vetinh.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["act"]))
            {
                this.act = this.comm.TextFormat(base.Request.QueryString["act"]).Trim();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["id"]))
            {
                this.id = this.comm.TextFormat(base.Request.QueryString["id"]).Trim();
                this.txtMa.Text = this.comm.TextFormat(this.comm.TextFormat(this.id));
            }
            if (base.Request.Params["type"] != null)
            {
                if (base.Request.Params["type"].ToString().Trim().Equals("san"))
                {
                    this.frm_sanvieclam.Visible = true;
                    this.frm_trungtamvetinh.Visible = false;
                    this.LoadVetinh();
                    if ((!base.IsPostBack && !string.IsNullOrEmpty(this.act)) && !this.id.Trim().Equals(""))
                    {
                        this.LoadInfo(this.id);
                    }
                }
                if (base.Request.Params["type"].ToString().Trim().Equals("vt"))
                {
                    this.frm_sanvieclam.Visible = false;
                    this.frm_trungtamvetinh.Visible = true;
                    if ((!base.IsPostBack && !string.IsNullOrEmpty(this.act)) && !this.id.Trim().Equals(""))
                    {
                        this.LoadInfoVeinh(this.id);
                    }
                }
            }
        }
        catch (Exception exception)
        {
            this.mess = "Lỗi hệ thống - " + exception.Message;
        }
    }
}
